<?php
namespace Admin\Controller;
use Think\Controller;
header('content-type:text/html;charset=utf-8');
class CourseController extends AdminController{
	function __construct(){
		parent::__construct();
		
		/* if(!$_SESSION['home']['user']){
			echo "<script>alert('很抱歉，您尚未登录﻿ ');window.location.href='".__APP__."/Admin/Index/login'</script>";
		} */
		
	}
	
	//课程首页
	public function public_list(){
		
		$id=I("get.sign");//首级ID
		
		
		if($id==0){
			//为公开课
			if($_POST){
				$where=I('post.');
				$this->assign("where",$where);
					
				foreach( $where as $key=>$val){
					if($val!==""){
						$data[$key]=$val;
					}
						
				}
			
			}
			$cond=$data;
			$class_1=M("course_list")->where("pid=0")->order("paixu asc")->select();//最大分类
			$this->assign("class_1",$class_1);//用于搜索用
			$pid=$class_1[0]['id'];
			$class_two=M("course_list")->where("pid=$pid")->order("paixu asc")->select();
			$this->assign("class_two",$class_two);
			$action="公开课列表";
			$cond['if_public']=1;
			
			
		}else{
			$course=M("course_list")->where("id=$id")->find();
			$action=$course['name']."列表";
			$class=M("course_list")->where("pid=$id")->order("paixu asc")->select();
			$this->assign("class",$class);//分类信息
			
			$model=M("course");
			if($_POST){
				$where=I('post.');
				$this->assign("where",$where);
					
				foreach( $where as $key=>$val){
					if($val!==""){
						$data[$key]=$val;
					}
						
				}
				$cond=$data;
				$cond['type_one']=$id;
			}else{
				$id_array=array();
				if($class){
					//说明有下级分类
					foreach($class as $key=>$value){
						$id_array[$key]=$value['id'];
					}
					$cond['type_two']=array("in",$id_array);
				}else{
					//说明无下级分类
					$cond['type_one']=$id;
					$if_class=1;//代表无下级分类
					$this->assign("if_class",$if_class);
				}
			}
			
		}
/* 		$res=M("course")->where($cond)->select();
		var_dump($where);
		var_dump(M()->getlastsql()); */
		$this->assign("action",$action);//操作名称
		$info=getpage("course",10,$cond,"start_time desc");//课程信息
		$this->assign("list",$info[0]);
		$this->assign("page",$info[1]);
		$this->display();
	}
	//添加修改课程
	public function add_course(){
		$model=M("course");
		$class_one=M("course_list")->where("pid=0")->order("paixu asc")->select();//一级分类
		$this->assign("class",$class_one);
		if($_POST){
			//表单提交
			
			$post=I("post.");
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize = 3145728 ;// 设置附件上传大小
			$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
			$upload->savePath = ''; // 设置附件上传（子）目录
			// 上传文件
			$info = $upload->upload();
			if($info) {
// 				// 上传错误提示错误信息
// 				$this->error($upload->getError());
// 			}else{
				$post['course_logo']="Public/Uploads/".$info['course_logo']['savepath'].$info['course_logo']['savename'];
			}
			
			if(!$_POST['type']){
				//添加
				$post['add_time']=date("Y-m-d H:i:s",time());
				$model->create($post);
				$res=$model->add();
				if($res){
					echo "<script>alert('添加成功');window.location.href='add_course'</script>";
					//$this->redirect("Course/public_list");
				}else{
					$url=$_SERVER['HTTP_REFERER'];
					echo "<script>alert('添加失败');window.location.href='".$url."'</script>";
				}
			}else{
				$sign=$_POST['sign'];
				$model->create($post);
				$res=$model->save();
				if($res){
					echo "<script>alert('修改成功');window.location.href='public_list?sign=".$sign."'</script>";
					
				}else{
					$url=$_SERVER['HTTP_REFERER'];
					echo "<script>alert('修改失败');window.location.href='".$url."'</script>";
				}
			}
		}else{
			//进入页面
			if(!$_GET['type']){
				//进入添加页面
				$action="课程添加";
				$pid=$class_one[0]['id'];
				$class_two=M("course_list")->where("pid=$pid")->order("paixu asc")->select();
				$this->assign("class_two",$class_two);
			}else{
				//进入修改页面
				$id=I("get.id");
				$action="课程修改";
				$info=$model->where("id=$id")->find();
				//查找二级同级分类
				$pid=$info['type_one'];
				$this->assign("info",$info);
				
			}
			$class_two=M("course_list")->where("pid=$pid")->order("paixu asc")->select();
			$this->assign("class_two",$class_two);
			
			$this->assign("action",$action);
			$this->display();
		}
		
	}
	//ajax查询分类
	public function sel_class(){
		$model=M("course_list");
		$pid=I("post.pid");
		$info=M("course_list")->where("pid=$pid")->order("paixu asc")->select();
		echo json_encode($info);
	}
	//添加分类
	public function add_class(){
	$model=M("course_list");
		if($_POST){
			$sign=I("post.sign");//代表所属课程
			//表单提交
			$post=I("post.");
			$post['pid']=$sign;
			if(!$_POST['type']){
				//添加
				
				$model->create($post);
				$res=$model->add();
				if($res){
					echo "<script>alert('添加成功');window.location.href='public_list?sign=".$sign."'</script>";
					//$this->redirect("Course/public_list");
				}else{
					$url=$_SERVER['HTTP_REFERER'];
					echo "<script>alert('添加失败');window.location.href='".$url."'</script>";
				}
			}else{
				//修改
				$model->create($post);
				$res=$model->save();
				if($res){
					echo "<script>alert('修改成功');window.location.href='public_list?sign=".$sign."'</script>";
					
				}else{
					$url=$_SERVER['HTTP_REFERER'];
					echo "<script>alert('修改失败');window.location.href='".$url."'</script>";
				}
			}
		}else{
			//进入页面
			if(!$_GET['type']){
				//进入添加页面
				$action="分类添加";
					
			}else{
				//进入修改页面
				$id=I("get.id");
				$action="分类修改";
				$info=$model->where("id=$id")->find();
				$this->assign("info",$info);
				
			}
			$this->assign("action",$action);
			$this->display();
		}
	}
	//报名人员信息
	public function class_staff(){
		$cond=array();
		if($_POST['user_name']){
			$cond['user_name']=I("post.user_name");
			$this->assign("user_name",$cond['user_name']);
		}
		$cond['c_id']=I("get.c_id");
		$info=getpage("class_staff",10,$cond,"enroll_time desc");
		$this->assign("info",$info);
		$url=$_SERVER['HTTP_REFERER'];
		$this->assign("url",$url);
		$this->display();
		
	}
	//课程留言
	public function comment(){
		$cond=array();
		if($_POST['course_name']){
			$cond['course_name']=I("post.course_name");
			$this->assign("course_name",$cond['course_name']);
		}
		if(I("get.c_id")){
			$cond['c_id']=I("get.c_id");
		}
		$info=getpage("comment",10,$cond,"add_time desc");//收藏信息
		$this->assign("info",$info);
		$this->display();
		
	}
	//课程收藏
	public function collect(){
		//$model=M("collect");
		$cond=array();
		if($_POST['course_name']){
			$cond['course_name']=I("post.course_name");
			$this->assign("course_name",$cond['course_name']);
		}
		if(I("get.c_id")){
			$cond['c_id']=I("get.c_id");
		}
		
		$info=getpage("course_collect",10,$cond,"add_time desc");//收藏信息
		$this->assign("info",$info);
		$this->display();
	}
	//删除
	public function del(){
		$table=I("post.table");
		$id=I("post.id");
		$model=M($table);
		$res=$model->where("id=$id")->delete();
		if($res){
			echo $_SERVER['HTTP_REFERER'];
		}
 		/* echo $_SERVER['HTTP_REFERER'];
 		die(); */
	}
	
	
}